package mobi.pruss.multiuser;

import java.util.ArrayList;
import android.widget.CheckBox;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class GetApps extends AsyncTask<Void, Void, List<MyApplicationInfo>> {
	final PackageManager pm;
	final Context	 context;
	final ListView listView;
	final ProgressBar progress;
	
	GetApps(Context c, ListView lv, ProgressBar progress2) {
		context = c;
		pm = context.getPackageManager();
		listView = lv;
		progress = progress2;
	}
	
	private boolean profilable(ApplicationInfo a) {		
		return a.publicSourceDir != null &&
			   ! a.packageName.equals(context.getPackageName());
	}

	@Override
	protected List<MyApplicationInfo> doInBackground(Void... c) {
		Log.v("getting", "installed");

		List<ApplicationInfo> list = 
			pm.getInstalledApplications(0);
		
		List<MyApplicationInfo> myList = new ArrayList<MyApplicationInfo>();
		
		MyCache cache = new MyCache(MyCache.genFilename(context, "app_labels"));
		
		for (int i = 0 ; i < list.size() ; i++) {
			if (profilable(list.get(i))) {
				MyApplicationInfo myAppInfo;
				myAppInfo = new MyApplicationInfo(
						cache, pm, list.get(i));
				myList.add(myAppInfo);
			}
		}
		cache.commit();
		
		return myList;
	}
	
	@Override
	protected void onPreExecute() {
		listView.setVisibility(View.GONE);
		progress.setVisibility(View.VISIBLE);
	}
	
	@Override
	protected void onPostExecute(final List<MyApplicationInfo> appInfo) {
		
		ArrayAdapter<MyApplicationInfo> appInfoAdapter = 
			new ArrayAdapter<MyApplicationInfo>(context, 
					R.layout.oneline, 
					appInfo) {

			public View getView(int position, View convertView, ViewGroup parent) {
				View v;				
				
				if (convertView == null) {
	                v = View.inflate(context, R.layout.oneline, null);
	            }
				else {
					v = convertView;
				}

				final MyApplicationInfo a = appInfo.get(position); 
				CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox);
				cb.setText(a.getLabel());
				cb.setOnCheckedChangeListener(null);
				cb.setChecked(
						(context.getSharedPreferences(Apps.PREF_APPS, 0))
						.getBoolean(a.packageName, false));
				cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked)
							((Apps)context).activateApp(a);
						else
							((Apps)context).deactivateApp(a);
					}
				});
				return v;
			}				
		};
		
		appInfoAdapter.sort(MyApplicationInfo.LabelComparator);
		listView.setAdapter(appInfoAdapter);
		
		progress.setVisibility(View.GONE);
		listView.setVisibility(View.VISIBLE);
	}
}
